package com.demo2do.wechatmcs.dao;

import com.demo2do.core.persistence.annotation.Condition;
import com.demo2do.core.persistence.annotation.Dao;
import com.demo2do.core.persistence.annotation.QueryTarget;
import com.demo2do.core.persistence.annotation.Save;
import com.demo2do.core.persistence.annotation.Update;
import com.demo2do.core.persistence.annotation.Variable;
import com.demo2do.wechatmcs.entity.webauth.WebAuthClient;
import com.demo2do.wechatmcs.entity.webauth.WebAuth;

/**
 * 
 * @author mohanlan
 *
 */
@Dao
public interface WebAuthDao {

    /**
     * 
     * @param host
     * @return
     */
    @QueryTarget(WebAuthClient.class)
    @Condition("WHERE host = :host")
    public WebAuthClient loadWebAuthClient(@Variable("host") String host);
    
    /**
     * 
     * @param webAuthRequest
     */
    @Save("INSERT INTO web_auth(id, request_no, state, client, redirect_url, scope, request_time, is_visited) VALUES(:id, :requestNo, :state, :client, :redirectUrl, :scope, :requestTime, :visited)")
    public void saveWebAuth(WebAuth webAuthRequest);

    /**
     * 
     * @param id
     * @return
     */
    @QueryTarget(WebAuth.class)
    @Condition("WHERE id = :id")
    public WebAuth loadWebAuth(@Variable("id") String id);

    
    /**
     * 
     * @param state
     * @return
     */
    @QueryTarget(WebAuth.class)
    @Condition("WHERE state = :state")
    public WebAuth getWebAuth(@Variable("state") String state);


    /**
     * 
     * @param id
     * @param openid
     */
    @Update("UPDATE web_auth SET openid = :openid, is_visited = true WHERE id = :id")
    public void authorize(@Variable("id") String id, @Variable("openid") String openid);

    /**
     * 
     * @param id
     * @param unionid
     * @param account
     */
    @Update("UPDATE web_auth SET unionid = :unionid, account_id = :account WHERE id = :id")
    public void union(@Variable("id") String id, @Variable("unionid") String unionid, @Variable("account") Long account);
    
    
}
